package com.itany.book.utils;


import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.net.URI;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HttpClientUtils {

    public static String doPost(String url, Map<String,String> params){
        //创建httpclient
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String result ="";
        CloseableHttpResponse response =null;
        try {
            //创建请求对象
            HttpPost httpPost = new HttpPost(url);
            //构建参数
            if(params!=null){
                List<NameValuePair> paramList = new ArrayList<NameValuePair>();
                for(Map.Entry<String,String> param:params.entrySet()){
                    paramList.add(new BasicNameValuePair(param.getKey(),param.getValue()));
                }

                //模拟form表单
                UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(paramList, Charset.defaultCharset());
                httpPost.setEntity(formEntity);
            }

            response = httpClient.execute(httpPost);
            result = EntityUtils.toString(response.getEntity(), Charset.defaultCharset());

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            close(httpClient,response);
        }
        return result;

    }

    public static String doGet(String url, Map<String,String> params){
        //创建httpclient
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String result ="";
        CloseableHttpResponse response =null;
        try {

            URIBuilder builder = new URIBuilder(url);
            //构建参数
            if(params!=null){
                List<NameValuePair> paramList = new ArrayList<NameValuePair>();
                for(Map.Entry<String,String> param:params.entrySet()){
                    builder.addParameter(param.getKey(),param.getValue());
                }

            }
            URI uri = builder.build();
            HttpGet httpGet = new HttpGet(uri);
            response= httpClient.execute(httpGet);
            result = EntityUtils.toString(response.getEntity(), Charset.defaultCharset());

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            close(httpClient,response);
        }
        return result;
    }


    public static void close(CloseableHttpClient httpClient,CloseableHttpResponse response){
             if(response!=null){
                 try {
                     response.close();
                 }catch (Exception e){
                     e.printStackTrace();
                 }

             }

        if(httpClient!=null){
            try {
                httpClient.close();
            }catch (Exception e){
                e.printStackTrace();
            }

        }
    }

    public static void main(String[] args) {
       /* Map<String,String> params = new HashMap<String, String>();
        params.put("mobile","555555");
        params.put("tpl_id","67040");
        params.put("tpl_value","%23code%23%3d123456");
        params.put("key","eefb806d68ebe16694cc0083cc9f46f0");
        params.put("dtype","json");
        System.out.println(HttpClientUtils.doPost("http://v.juhe.cn/sms/send",params));*/



        // Map<String,String> params = new HashMap<String, String>();
        // params.put("key","af8f9807ad65ece00d5aa8a32d209bff");
        // params.put("sub","9787020008728");
        // System.out.println(HttpClientUtils.doGet("http://feedback.api.juhe.cn/ISBN",params));


    }

}
